package com.heifeng.cloudshelf.push;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;

import com.google.gson.Gson;
import com.heifeng.cloudshelf.constants.HTTPConstants;
import com.heifeng.cloudshelf.dao.PushBean;
import com.heifeng.cloudshelf.event.PushEvent;
import com.heifeng.cloudshelf.mvp.main.activity.MainActivity;
import com.heifeng.cloudshelf.network.NetClient;
import com.heifeng.cloudshelf.utils.ToastUitl;
import com.orhanobut.hawk.Hawk;

import org.greenrobot.eventbus.EventBus;

import cn.jpush.android.api.CmdMessage;
import cn.jpush.android.api.CustomMessage;
import cn.jpush.android.api.JPushMessage;
import cn.jpush.android.api.NotificationMessage;
import cn.jpush.android.service.JPushMessageReceiver;

public class PushMessageReceiver extends JPushMessageReceiver {
    private static final String TAG = "PushMessageReceiver";

//    private static OnMessageReceiverListener onMessageReceiverListener;

//    public static void setOnMessageReceiverListener(OnMessageReceiverListener onMessageReceiverListener) {
//        PushMessageReceiver.onMessageReceiverListener = onMessageReceiverListener;
//    }

    @Override
    public void onMessage(Context context, CustomMessage customMessage) {
        Log.e(TAG, "[onMessage] " + customMessage);
        //processCustomMessage(context,customMessage);
//        if (null != onMessageReceiverListener) {
//            onMessageReceiverListener.onCustomMessage(context, customMessage);
//        }
    }

    @Override
    public void onNotifyMessageOpened(Context context, NotificationMessage message) {
        Log.e(TAG, "[onNotifyMessageOpened] " + message);
//        if (null != onMessageReceiverListener) {
//            onMessageReceiverListener.onNotifyMessageOpened(context, message);
//        }
    }

    @Override
    public void onMultiActionClicked(Context context, Intent intent) {
        Log.e(TAG, "[onMultiActionClicked] 用户点击了通知栏按钮");
//        if (null != onMessageReceiverListener) {
//            onMessageReceiverListener.onMultiActionClicked(context, intent);
//        }
//        String nActionExtra = intent.getExtras().getString(JPushInterface.EXTRA_NOTIFICATION_ACTION_EXTRA);
//        //开发者根据不同 Action 携带的 extra 字段来分配不同的动作。
//        if(nActionExtra==null){
//            Log.d(TAG,"ACTION_NOTIFICATION_CLICK_ACTION nActionExtra is null");
//            return;
//        }
//        if (nActionExtra.equals("my_extra1")) {
//            Log.e(TAG, "[onMultiActionClicked] 用户点击通知栏按钮一");
//        } else if (nActionExtra.equals("my_extra2")) {
//            Log.e(TAG, "[onMultiActionClicked] 用户点击通知栏按钮二");
//        } else if (nActionExtra.equals("my_extra3")) {
//            Log.e(TAG, "[onMultiActionClicked] 用户点击通知栏按钮三");
//        } else {
//            Log.e(TAG, "[onMultiActionClicked] 用户点击通知栏按钮未定义");
//        }
    }

    @Override
    public void onNotifyMessageArrived(Context context, NotificationMessage message) {
        Log.e(TAG, "[onNotifyMessageArrived] " + message);
//        if (null != onMessageReceiverListener) {
//            onMessageReceiverListener.onNotifyMessageArrived(context, message);
//        }
        //接受到的信息
        Log.e("JPushMessageService", "onNotifyMessageArrived:" + message);
        try {
            PushBean pushBean = new Gson().fromJson(message.notificationExtras, PushBean.class);
            Log.e("JPushMessageService", "push:" + pushBean.toString());
            if (TextUtils.equals(pushBean.getType(), "1")) {
                NetClient.cliId = "";
                NetClient.token = "";
                HTTPConstants.userId = "";
                HTTPConstants.isLogined = false;
                Hawk.delete(HTTPConstants.USER_ID_KEY);
                Hawk.delete(HTTPConstants.TOKEN_KEY);
                Hawk.delete(HTTPConstants.CLIID_KEY);
                context.startActivity(new Intent(context, MainActivity.class));
                ToastUitl.showLong(pushBean.getContent());
            } else if (TextUtils.equals(pushBean.getType(), "2") || TextUtils.equals(pushBean.getType(), "3")) {
                Log.e("JPushMessageService", "type:" + pushBean.getType());
                EventBus.getDefault().post(new PushEvent(pushBean));
            }
        } catch (Exception ignored) {
        }
    }

    @Override
    public void onNotifyMessageDismiss(Context context, NotificationMessage message) {
        Log.e(TAG, "[onNotifyMessageDismiss] " + message);
//        if (null != onMessageReceiverListener) {
//            onMessageReceiverListener.onNotifyMessageDismiss(context, message);
//        }
    }

    @Override
    public void onRegister(Context context, String registrationId) {
        Log.e(TAG, "[onRegister] " + registrationId);
//        if (null != onMessageReceiverListener) {
//            onMessageReceiverListener.onRegister(context, registrationId);
//        }
    }

    @Override
    public void onConnected(Context context, boolean isConnected) {
        Log.e(TAG, "[onConnected] " + isConnected);
//        if (null != onMessageReceiverListener) {
//            onMessageReceiverListener.onConnected(context, isConnected);
//        }
    }

    @Override
    public void onCommandResult(Context context, CmdMessage cmdMessage) {
        Log.e(TAG, "[onCommandResult] " + cmdMessage);
//        if (null != onMessageReceiverListener) {
//            onMessageReceiverListener.onCommandResult(context, cmdMessage);
//        }
    }

    @Override
    public void onTagOperatorResult(Context context, JPushMessage jPushMessage) {
        TagAliasOperatorHelper.getInstance().onTagOperatorResult(context, jPushMessage);
        super.onTagOperatorResult(context, jPushMessage);
    }

    @Override
    public void onCheckTagOperatorResult(Context context, JPushMessage jPushMessage) {
        TagAliasOperatorHelper.getInstance().onCheckTagOperatorResult(context, jPushMessage);
        super.onCheckTagOperatorResult(context, jPushMessage);
    }

    @Override
    public void onAliasOperatorResult(Context context, JPushMessage jPushMessage) {
        TagAliasOperatorHelper.getInstance().onAliasOperatorResult(context, jPushMessage);
        super.onAliasOperatorResult(context, jPushMessage);
    }

    @Override
    public void onMobileNumberOperatorResult(Context context, JPushMessage jPushMessage) {
        TagAliasOperatorHelper.getInstance().onMobileNumberOperatorResult(context, jPushMessage);
        super.onMobileNumberOperatorResult(context, jPushMessage);
    }

    //send msg to MainActivity
//    private void processCustomMessage(Context context, CustomMessage customMessage) {
//        if (MainActivity.isForeground) {
//            String message = customMessage.message;
//            String extras = customMessage.extra;
//            Intent msgIntent = new Intent(MainActivity.MESSAGE_RECEIVED_ACTION);
//            msgIntent.putExtra(MainActivity.KEY_MESSAGE, message);
//            if (!ExampleUtil.isEmpty(extras)) {
//                try {
//                    JSONObject extraJson = new JSONObject(extras);
//                    if (extraJson.length() > 0) {
//                        msgIntent.putExtra(MainActivity.KEY_EXTRAS, extras);
//                    }
//                } catch (JSONException e) {
//
//                }
//
//            }
//            LocalBroadcastManager.getInstance(context).sendBroadcast(msgIntent);
//        }
//    }


}
